package com.example.sqlite.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.sqlite.dao.ContactsDAO;
import com.example.sqlite.utils.EditMask;

public class AddContactActivity extends Activity {

	private ContactsDAO dao;

	private Button buttonAddContact;
	private Button buttonCancel;
	private EditText editTextNameContact;
	private EditText editTextFoneContact;

	private final OnClickListener onClickListenerButtonAddContact = new OnClickListener() {

		@Override
		public void onClick( View view ) {
			String name = editTextNameContact.getEditableText().toString();
			String fone = editTextFoneContact.getEditableText().toString();
			if ( !name.isEmpty() && !fone.isEmpty() ) {
				dao.create( name, fone );
				finish();
				//
			} else
				editTextNameContact.setBackgroundColor( android.R.color.holo_red_light );
		}
	};

	private final OnClickListener onClickListenerButtonCancel = new OnClickListener() {

		@Override
		public void onClick( View arg0 ) {
			onBackPressed();
		}
	};

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_add_contact );

		dao = new ContactsDAO( this );
		dao.open();

		buttonAddContact = (Button) findViewById( R.id.button_add );
		buttonCancel = (Button) findViewById( R.id.button_cancel );
		editTextNameContact = (EditText) findViewById( R.id.edit_text_name );
		editTextFoneContact = (EditText) findViewById( R.id.edit_text_fone );

		editTextFoneContact.addTextChangedListener( new EditMask( editTextFoneContact, "###.###.###-##" ) );

		buttonAddContact.setOnClickListener( onClickListenerButtonAddContact );
		buttonCancel.setOnClickListener( onClickListenerButtonCancel );

	}

	@Override
	protected void onResume() {
		dao.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		dao.close();
		super.onPause();
	}

}
